/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : Sum.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 3, 2009  Raghu Sarraff	 		        Created
 */
package lwf.handler.entities.math;

import java.util.List;

import net.lwf.handler.constant.IHandlerConstant;


public class Sum {
	
	private String operation;
	private List<Double> data; //For all operations, the first element[0] is lesser than than the second element[1]
	//private Object answer;
	//private int[] devideans; //will have two elements. first one[0] is quotient and the secondone[1] is the remainder
	private int decimalDigits;
	//customized ones

	/**
	 * @param operation
	 * @param data
	 */
	public Sum(String operation, int decimalDigits) {
		this.operation = operation;
		this.decimalDigits = decimalDigits;
	}
	
	/**
	 * @param data The data to set.
	 */
	public void setData(List<Double> data) {
		this.data = data;
	}
	/**
	 * @return Returns the answer.
	 */
	public Object getAnswer() {
		Object object = SumUtil.getAnswer(operation, data, this.decimalDigits);
		//this.processAnswer(); //commented out on 01/26/11
		//return answer;
		if(operation.equals(IHandlerConstant.DIVISION)) {
			
			if(decimalDigits == 0) {
				double[] devideans = (double[]) object;
				//System.out.println(String.valueOf(devideans[1]) + "|" + String.valueOf(devideans[0]));
				return String.valueOf(devideans[0]) + "|" + String.valueOf(devideans[1]);
			}

		}
		
		return object;
	}
	
//	private hasDecimaDigits() {
//		for(Double displaydata: data) {
//			String value = String.valueOf(displaydata);
//			int dec_position = value.indexOf(".");
//			value = value.substring(dec_position);
//			if(!value.equals("0") && value.length() > )
//		}
//		return false;
//	}
	/**
	 * @return Returns the data.
	 */
	public List<Double> getData() {
		return data;
	}
	/**
	 * @return Returns the operation.
	 */
	public String getOperation() {
		return operation;
	}
	
	
	public void print() {
		System.out.println("========> Input: Operation: " + operation);
		for(int i=0; i<data.size(); i++) {
			System.out.println(data.get(i));
		}
		System.out.println(" ");
		
		Object obj = getAnswer();
		System.out.println(" Answer: " + obj);
//		if(!operation.equals(IHandlerConstant.DIVISION)) {
//			System.out.println(" Answer: " + obj);
//		}
//		else {
//			double[] devideans = (double[]) obj;
//			System.out.println("Quotient: " + devideans[0] + " AND Remainder: " + devideans[1]);
//		}
		
	}
	
	//----------------------
	// Customized
	//------------
	/**
	 * @return Returns the devideans.
	 */
/*	
    public int[] getDevideans() {
		this.processAnswer();
		return devideans;
	}

	public void print() {
		System.out.println("========> Input: Operation: " + this.operation);
		for(int i=0; i<data.size(); i++) {
			System.out.println(data.get(i));
		}
		System.out.println(" ");
		
		//getAnswer();
		this.processAnswer();
		if(!this.operation.equals(IHandlerConstant.DIVISION)) {
			System.out.println(" Answer: " + this.answer);
		}
		else {
			System.out.println("Quotient: " + devideans[0] + " AND Remainder: " + devideans[1]);
		}
		
	}
	

	// private
	private void processAnswer() {
		if(this.operation.equals(IHandlerConstant.ADDITION)) {
			this.processAdd();
		}
		else if(this.operation.equals(IHandlerConstant.SUBTRACTION)) {
			this.processSubtract();
		}
		else if(this.operation.equals(IHandlerConstant.MULTIPLICATION)) {
			this.processMultiplication();
		}
		else {
			//Division
			this.processDevide();
			
		}		
	}

	private void processAdd() {
		double temp = 0;
		for(int i=0; i<data.size(); i++) {
			temp += ((Double) data.get(i)).doubleValue();
		}
		this.answer = new Double(temp);
	}
	
	private void processSubtract() {
		double temp = ((Double) data.get(1)).doubleValue() - ((Double) data.get(0)).doubleValue();
		this.answer = new Double(temp);
	}
	
	private void processMultiplication() {
		double temp = ((Double) data.get(1)).doubleValue() * ((Double) data.get(0)).doubleValue();
		this.answer = new Double(temp);
	}
	
	private void processDevide() {
		double val1 = ((Double) data.get(1)).doubleValue();
		double val2 = ((Double) data.get(0)).doubleValue();
		devideans = new int[2];
		
		//For Quotient
		try {
			System.out.println("=========> Quotient: " + val1/val2);
			devideans[0] = (int)val1/ (int)val2;
		}
		catch(ArithmeticException aex) {
			devideans[0] = 0;
		}
		//For Remainder
		try {
			System.out.println("=========> Remainder: " + val1%val2);
			devideans[1] = (int)val1 % (int)val2;
		}
		catch(ArithmeticException aex) {
			devideans[1] = 0;
		}
		this.answer = devideans;
	}*/
	
}

